package com.wbu.estatemanagement.controller;

import com.github.pagehelper.Page;
import com.wbu.estatemanagement.common.MessageConstant;
import com.wbu.estatemanagement.common.PageResult;
import com.wbu.estatemanagement.common.Result;
import com.wbu.estatemanagement.common.StatusCode;
import com.wbu.estatemanagement.domain.Activity;
import com.wbu.estatemanagement.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("activity")
public class ActivityController {
    @Autowired
    ActivityService activityService;

    @PostMapping("/userActivity")
    public Result userActivity(){
        List<Activity> activityList= activityService.findActivity();
        return new Result(true, StatusCode.OK, MessageConstant.SUCCESS,activityList);
    }

    @RequestMapping("/search")
    public PageResult search(@RequestBody Map searchMap){
        Page<Activity> page = activityService.search(searchMap);
        return new PageResult(true, StatusCode.OK, MessageConstant.SUCCESS,page.getResult(),page.getTotal());

    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        Activity activity = activityService.findById(id);
        return new Result(true,StatusCode.OK,MessageConstant.SUCCESS,activity);
    }


    @PostMapping("/add")
    public Result add(@RequestBody Activity activity){
        Boolean add = activityService.add(activity);
        if (add==false){
            System.out.println(" activity  is  null");
        }
        return new Result(true,StatusCode.OK,MessageConstant.SUCCESS);
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Activity activity){
        Boolean update = activityService.update(activity);
        return new Result(true,StatusCode.OK,MessageConstant.SUCCESS);
    }

    // /community/updateStatus/0/1
    @RequestMapping("/updateStatus/{status}/{id}")
    public Result updateStatus(@PathVariable("status") String status, @PathVariable("id") Integer id){
        Boolean flag = activityService.updateStatus(status,id);
        return new Result(true,StatusCode.OK,MessageConstant.SUCCESS);
    }

    @RequestMapping("/del")
    public Result del(@RequestBody List<Integer> ids){
        Boolean flag = activityService.del(ids);
        return new Result(true,StatusCode.OK,MessageConstant.SUCCESS);
    }
}
